<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT      : rubik-base
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * </pre>
 * 
 * @version 		1.0
 * @author			Dang Nhon Hoa <hoadn@rubik.com.vn>
 * @access			public
 * @package			rubik.base.html
 *
 * Created on	: May 25, 2008
 */
RCoreImporter::cls( "html.BasicHtmlUploadControl" );

class BasicHtmlImageUploadControl extends BasicHtmlUploadControl {
	
	var $_width;
	var $_height;
	
	/**
	 * Construtor
	 *
	 * @param string $inputName
	 * @param string $pathFromRoot Ex: "uploadFiles/images/"
	 * @param string/array $valueOrArrayContain if array normaly it should be: $_GET, $_POST, $_REQUEST
	 * @param string $supportedTypes Ex: ".mp3, .swf, .wma" 
	 * @return AbstractHtmlUploadForm
	 */
	function __construct($inputName, $pathFromRoot, $valueOrArrayContain = null, $width = null, $height = null, $supportedTypes = ".jpeg, .jpg, .gif, .png") {
		parent::__construct ( $inputName, $pathFromRoot, $valueOrArrayContain, $supportedTypes );
		$this->_width = $width;
		$this->_height = $height;
	}
	
	/**
	 * Set value for this
	 *
	 * @param string/array $valueOrArrayContain
	 */
	function setValue($valueOrArrayContain) {
		if (! is_array ( $valueOrArrayContain )) {
			$this->_value = $valueOrArrayContain;
			return;
		}
		
		$inputName = $this->_inputName;
		$oldName = "old_upload_{$inputName}";
		
		$oldLink = $valueOrArrayContain [$oldName];
		$oldLink = base64_decode ( $oldLink );
		if (! empty ( $oldLink ) && strpos ( $oldLink, "://" ) === false) {
			@unlink ( $this->getAbsRootPath () . $oldLink );
		}
		
		$file_name = $_FILES [$inputName] ['name'];
		$temp_file = $_FILES [$inputName] ['tmp_name'];
		$folder = $this->getAbsRootPath () . $this->_pathFromRoot;
		$sUploadValue = RFileFolderHelper::uploadFile ( $temp_file, $file_name, $folder, $this->_supportedTypes );
		if ($sUploadValue === false) {
			$this->setError ( 'Upload picture error!' );
		} else {
			$sUploadValue = $folder . $sUploadValue;
			
			if (! empty ( $this->_width ) && ! empty ( $this->_height )) {
				RImageHelper::createThumbnail ( $sUploadValue, $sUploadValue, $this->_width, $this->_height );
			}
		}
		
		$this->_value = $sUploadValue;
		return $sUploadValue;
	}
}
?>